home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / etc / lf / entry.cc < prev    next >
C/C++ Source or Header  |  1994-05-13  |  2KB  |  94 lines

  1. /* Manipulate directory entries of a particular file class. */
  2. #include <std.h>
  3. #include "entry.h"
  4.  
  5. /* Initially provide a medium-sized file entry size. */
  6. const int Entry_Handler::default_entries = 50;
  7.  
  8. /* Print entries to standard output.  The algorithm used to format the files
  9.    in columns is subtle, and worth studying in some detail. */
  10.  
  11. void
  12. Entry_Handler::print_entries (char *class_name) 
  13. {
  14.   const int  width = Screen_Handler::screen_width ();
  15.   const int  ncols = width / (max_entry_length + 1);
  16.   const int  nrows = (entries + ncols - 1) / ncols;
  17.   const int  max   = nrows * (entries - (nrows - 1) * ncols);
  18. #if defined (__GNUC__) && ! defined (__STRICT_ANSI__)
  19.   char buffer[width];
  20. #else
  21.   char *buffer = new char[width];
  22. #endif
  23.  
  24.   /* Print out the file class name, nicely centered and in inverse video. */
  25.   sprintf (buffer, "[ %d %s File%s ]", entries, class_name, entries == 1 ? "" : "s");
  26.   Screen_Handler::print_inverse_centered (buffer);
  27.  
  28. #ifndef __GNUC__
  29.   delete [] buffer;
  30. #endif
  31.  
  32.   /* Take care of everything but the (possibly non-existent) final row. */
  33.   
  34.   for (int row = 0; row < nrows - 1; row++)
  35.     {
  36.       /* This loop is subtle, since we don't want to process too many entries.... */
  37.       
  38.       for (int col = row; col < entries; col += col < max ? nrows : nrows - 1)
  39.         printf ("%-*s", max_entry_length + 1, buf[col]);
  40.       
  41.       putchar ('\n');
  42.     }
  43.   /* Treat the final row specially, if it exists. */
  44.   
  45.   for (; row < max; row += nrows) 
  46.     printf ("%-*s", max_entry_length + 1, buf[row]);
  47.   
  48.   putchar ('\n');
  49. }
  50.  
  51. /* Only compile these functions if -O is *not* enabled. */
  52.  
  53. #ifndef __OPTIMIZE__
  54.  
  55. /* Initialize a new file class. */
  56.  
  57. Entry_Handler::Entry_Handler (void)
  58. {
  59.   entries = max_entry_length = 0;
  60.   total_entries = default_entries;
  61.   buf = new char *[default_entries];
  62. }
  63.  
  64. /* Current number of file entries. */
  65.  
  66. int 
  67. Entry_Handler::entry_number (void)
  68. {
  69.   return entries;
  70. }
  71.  
  72. /* Add an entry to the file class. */
  73.  
  74. void 
  75. Entry_Handler::add_entry (char *entry_name, int length)
  76. {
  77.   /* Grow the buffer on overflow. */
  78.   if (entries >= total_entries)
  79.     buf = new (buf, total_entries *= 2) char *;
  80.   if (max_entry_length < length)
  81.     max_entry_length = length;
  82.   buf[entries++] = strcpy (new char[length + 1], entry_name);
  83. }
  84.  
  85. /* Sort entries by filename. */
  86.  
  87. void 
  88. Entry_Handler::sort_entries (void)
  89. {
  90.   sort (buf, entries);
  91. }
  92.  
  93. #endif                          // __OPTIMIZE__
  94.